﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using JMailT.GMail;
using JMailT.Data;
using JMailT.Store;

namespace JMailTShell
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        GMailService _gmailServ = new GMailService();

        LocalStore _store = new LocalStore();

        public MainWindow()
        {
            InitializeComponent();

            _gmailServ.Connect("", 0, "", "");
        }

        private void _ctlBtnList_Click(object sender, RoutedEventArgs e)
        {
            _ctlListMails.ItemsSource = _gmailServ.Query();
        }

        protected override void OnClosed(EventArgs e)
        {
            _gmailServ.Disconnect();

            base.OnClosed(e);
        }

        private void _ctlListMails_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            LiteItem selItem = e.AddedItems[0] as LiteItem;

            Mail mail = _store.Load("gmail", "jaeho", selItem);
            if (mail == null)
            {
                mail = _gmailServ.Get(selItem);
                _store.Save("gmail", "jaeho", mail);
            }

            _ctlEditContent.Text = mail.Content;
        }

        private void _ctlBtnListCount_Click(object sender, RoutedEventArgs e)
        {
            label1.Content = _gmailServ.QueryCount().ToString();
        }
    }
}
